package liziy.spring.core;

import liziy.spring.beans.factory.config.AnnotationBeanNameGenerator;

import java.lang.annotation.Annotation;
import java.util.LinkedHashMap;

/**
 * 解析注解并转换为一个Map集合对象
 * @author Liziy
 * @date 2020/12/18 11:57
 **/
public class AnnotationAttributes extends LinkedHashMap<String, Object> {
    public static final String EMPTY_STRING = "";
    public AnnotationAttributes(Annotation annotation) {
        //解析注解对象
        resolverAnnotationAttributes(annotation);
    }

    private void resolverAnnotationAttributes(Annotation annotation) {
        //@liziy.spring.core.stereotype.Repository(value=dao)
        //把注解对象转换为一个字符串
        String annString = annotation.toString();
        int startIndex = annString.lastIndexOf("(") +1;
        int endIndex = annString.lastIndexOf(")");
        //得到两种结果 value = dao / value =
        annString = annString.substring(startIndex, endIndex);
        //截取字符串为数组
        String[] annElement = annString.split("=");
        //判断是否设置了value值
        if (annElement.length > 1) {
            this.put(annElement[0], annElement[1]);
        } else {
            this.put(annElement[0], EMPTY_STRING);
        }
    }
}
